package com.itheima.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.dao.QuestionDao;
import com.itheima.domain.Question;
import com.itheima.service.QuestionService;
import com.itheima.utils.MybatisUtil;
import org.apache.ibatis.session.SqlSession;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import javax.servlet.ServletOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class QuestionServiceImpl implements QuestionService {
    @Override
    public PageInfo<Question> findByPage(int currentPage, int pageSize) {
        SqlSession sqlSession = null;
        PageInfo<Question> info = null;
        try {
            //1 通过MybatisUtil工厂类获取SqlSession对象
            sqlSession = MybatisUtil.getSqlSession();
            //2 通过MybatisUtil工厂类获取dao接口的代理对象
            QuestionDao questionDao = MybatisUtil.getMapper(sqlSession, QuestionDao.class);
            //3 执行操作释放资源
            //3.1 在查询所有之前设置分页参数
            PageHelper.startPage(currentPage, pageSize);
            //3.2 查询所有
            List<Question> list = questionDao.findAll();
            //3.3 封装分页结果
            info = new PageInfo<>(list);
        } finally {
            //释放资源
            MybatisUtil.close(sqlSession);
        }
        return info;
    }

    @Override
    public void save(Question question) {
        SqlSession sqlSession = null;
        try {
            //1.通过MybatisUtil工厂类获取SQLSession对象
            sqlSession = MybatisUtil.getSqlSession();
            //2.通过MybatisUtil获取dao接口代理对象
            QuestionDao questionDao = MybatisUtil.getMapper(sqlSession, QuestionDao.class);
            //设置主键唯一id
            String id = UUID.randomUUID().toString().replace("-", "");
            question.setId(id);
            //3. 执行操作释放资源
            //3.1执行操作
            questionDao.save(question);
            //3.2提交事务
            MybatisUtil.commit(sqlSession);
        } finally {
            //释放资源
            MybatisUtil.close(sqlSession);
        }
    }

    @Override
    public Question findById(String questionId) {
        SqlSession sqlSession = null;
        Question question = null;
        try {
            //1 通过MybatisUtil工厂类获取SqlSession对象
            sqlSession = MybatisUtil.getSqlSession();
            //2 通过MybatisUtil工厂类获取dao接口的代理对象
            QuestionDao questionDao = MybatisUtil.getMapper(sqlSession, QuestionDao.class);
            //3 执行操作释放资源
            //3.1 在查询所有之前设置分页参数
            question = questionDao.findById(questionId);
        } finally {
            //释放资源
            MybatisUtil.close(sqlSession);
        }
        return question;
    }

    @Override
    public void edit(Question question) {
        SqlSession sqlSession = null;
        try {
            //1.通过MybatisUtil工厂类获取SQLSession对象
            sqlSession = MybatisUtil.getSqlSession();
            //2.通过MybatisUtil获取dao接口代理对象
            QuestionDao questionDao = MybatisUtil.getMapper(sqlSession, QuestionDao.class);
            //3. 执行操作释放资源
            //3.1执行操作
            questionDao.update(question);
            //3.2提交事务
            MybatisUtil.commit(sqlSession);
        } finally {
            //释放资源
            MybatisUtil.close(sqlSession);
        }
    }

    @Override
    public void delete(String[] ids) {
        SqlSession sqlSession = null;
        try {
            //1.通过MybatisUtil工厂类获取SQLSession对象
            sqlSession = MybatisUtil.getSqlSession();
            //2.通过MybatisUtil获取dao接口代理对象
            QuestionDao questionDao = MybatisUtil.getMapper(sqlSession, QuestionDao.class);
            //3. 执行操作释放资源
            //3.1执行操作
            Stream.of(ids).forEach(id -> questionDao.delete(id));
            //3.2提交事务
            MybatisUtil.commit(sqlSession);
        } finally {
            //释放资源
            MybatisUtil.close(sqlSession);
        }
    }

    @Override
    public List<Question> findAll() {
        SqlSession sqlSession = null;
        List<Question> list = null;
        try {
            //1 通过MybatisUtil工厂类获取SqlSession对象
            sqlSession = MybatisUtil.getSqlSession();
            //2 通过MybatisUtil工厂类获取dao接口的代理对象
            QuestionDao questionDao = MybatisUtil.getMapper(sqlSession, QuestionDao.class);
            //3 执行操作释放资源
            //3.1 在查询所有之前设置分页参数
            list = questionDao.findAll();
        } finally {
            //释放资源
            MybatisUtil.close(sqlSession);
        }
        return list;
    }

    @Override
    public void toExport(OutputStream outputStream) throws IOException {
        //1.获取模板文件的位置
        String path = this.getClass().getClassLoader().getResource("question_template.xlsx").getPath();
        //2.创建工作簿读取模板excel文件
        Workbook workbook = new XSSFWorkbook(path);
        //3.获取工作表
        Sheet sheet = workbook.getSheetAt(0);
        //4.调用findAll()方法,获取所有的问题对象集合
        List<Question> questionList = findAll();
        //5.转换集合,数组中存储的是每一道题的属性值 List<Question> ==> List<String[]>
        List<String[]> questions = questionList.stream()
                .map(question ->{
                    //创建字符串类型数组,存储question的属性值
                    String[] fields = new String[12];
                    //将question的属性值保存到数组中
                    fields[0] = question.getId();
                    fields[1] = question.getCompanyId();
                    fields[2] = question.getCatalogId();
                    fields[3] = question.getRemark();
                    fields[4] = question.getSubject();
                    fields[5] = question.getPicture();
                    fields[6] = question.getAnalysis();
                    fields[7] = question.getType();
                    fields[8] = question.getDifficulty();
                    fields[9] = question.getIsClassic();
                    fields[10] = question.getState();
                    fields[11] = question.getReviewStatus();
                    return fields;
                    /*String[] fields = question.toString().split("&");
                    return fields;*/
                })
                .collect(Collectors.toList());
        //6.遍历所有题目,创建row和cell,向cell中写数据
        //定义通用的样式
        CellStyle style = workbook.createCellStyle();
        style.setBorderTop(BorderStyle.THIN);
        style.setBorderBottom(BorderStyle.THIN);
        style.setBorderLeft(BorderStyle.THIN);
        style.setBorderRight(BorderStyle.THIN);
        //开始写数据
        int rowIndex=3;//起始行索引
        for (String[] fields : questions) {
            //每个问题创建一行
            Row row = sheet.createRow(rowIndex++);
            int cellIndex = 1;//从模板第一列开始
            for (String field : fields) {
                //每一个属性创建一列
                Cell cell = row.createCell(cellIndex++);
                //向单元格中写入数据
                cell.setCellValue(field);
                cell.setCellStyle(style);
            }
        }
        //7 将workbook中的内容输出
        workbook.write(outputStream);
        workbook.close();
    }

}
